﻿package algorithms {
	import events.DitherMatrixEvent;
	import flash.events.EventDispatcher;
	
	public class DitherMatrix extends EventDispatcher {
		
		private var _width:int;
		private var _height:int;
		private var _elements:Array;
		
		public function DitherMatrix(width:int = 2, height:int = 2, elements:Array = [0, 2, 1, 3]) {
			set(width, height, elements);
		}
		
		public function set(width:int, height:int, elements:Array):void {
			if (!elements) throw new Error("The elements array must not be null.");
			if (elements.length != width * height) throw new Error("The elements array length does not match the specified matrix dimension.");
			
			_width = width;
			_height = height;
			_elements = elements.concat();
			
			dispatchEvent(new DitherMatrixEvent(DitherMatrixEvent.CHANGE, this.clone()));
		}
		
		public function clone():DitherMatrix {
			return new DitherMatrix(_width, _height, _elements);
		}
	}
}